from . import ConnPG

class ChangePassword:

    async def change_password(self, phone, old_pwd, new_pwd):

        args = (phone, old_pwd, new_pwd)

        if (not all(args)) or len(new_pwd) > 32:

            return {
                'status': -1,
                'errorCode': 100107,
                'errorMsg': '参数有误，请检查参数',
                'resultBody': None
            }

        sql = 'select * from users where phone=%s and password=%s'

        result = await ConnPG().fetchone(sql, (phone, old_pwd))

        if result == None:

            return {
                'status': -1,
                'errorCode': 100103,
                'errorMsg': '请检查账号或密码',
                'resultBody': None
            }

        else:

            update_sql = 'update users set password=%s where id=%s'

            await ConnPG().execute(update_sql, (new_pwd, result['id']))

            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "密码修改成功",
            }



